
/**
 * 使用两个指针不断的循环，总会有一个地方相交
 * @param {*} head1 
 * @param {*} head2 
 * @returns 
 */
function xc(head1, head2) {
    if (!head1 || !head2) return false;
    let p1 = head1;
    let p2 = head2;
    while (p1 != p2) {
        p1 = p1 == null ? head1 : p1.next;
        p2 = p2 == null ? head2 : p2.next;
    }
}

/**
 * 先计算两个链表的长度，然后使得两个链表从同一处开始遍历
 * @param {*} head1 
 * @param {*} head2 
 * @returns 
 */
function xc1(head1, head2) {
    if (!head1 || !head2) return false;
    let p1 = head1;
    let p2 = head2;
    let num1 = 0, num2 = 0;
    while (p1) {
        num1++;
        p1 = p1.next;
    }
    while (p2) {
        num2++;
        p2 = p2.next;
    }
    p1 = head1;
    p2 = head2;
    if (num1 < num2) {
        for (let i = 0; i < num2 - num1; i++) {
            p2 = p2.next;
        }
    } else {
        for (let i = 0; i < num1 - num2; i++) {
            p1 = p1.next;
        }
    }
    while (p1 && p2) {
        if (p1 == p2) return true;
        p1 = p1.next;
        p2 = p2.next;
    }
    return false;
}